home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Jotto ][ 1.2 / source / Wipes reversed ƒ / Hilbert wipe reversed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  3.3 KB  |  120 lines  |  [TEXT/MMCC]

  1. #include "timing.h"
  2.  
  3. /* This fills the screen by a Hilbert space-filling curve, which is defined by
  4.    two mutually recursive drawing functions:
  5.    
  6.    X = left, Y, draw, right, X, draw, X, right, draw, Y, left
  7.    Y = right, X, draw, left, Y, draw, Y, left, draw, X, right
  8.    
  9.    Start by drawing X at the highest recursion level from the bottomleft of the
  10.    screen. (At recursion level 1, X and Y are null functions.)
  11.    
  12.    [Note that the procedure below actually draws the Hilbert curve flipped
  13.    horizontally on the screen, so it starts drawing at the bottomright and
  14.    works leftward.]
  15. */
  16.  
  17. #define    RecursionLevel    4
  18. #define CorrectTime 1
  19. #define theWindowHeight (boundsRect.bottom-boundsRect.top)
  20. #define theWindowWidth (boundsRect.right-boundsRect.left)
  21.  
  22. pascal void HilbertRecurseReversed(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect,
  23.     short level, short whichpattern, short* x, short* y);
  24. pascal short HilbertWipeReversed(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect);
  25.  
  26. typedef char    Hilby[11];
  27.  
  28. static Hilby    HilbertPattern[]=
  29. {
  30.     {
  31.         0x02,0x69,0x00,0x01,0x42,0x00,0x42,0x01,0x00,0x69,0x02
  32.     },
  33.     {
  34.         0x01,0x42,0x00,0x02,0x69,0x00,0x69,0x02,0x00,0x42,0x01
  35.     }
  36. };
  37.  
  38. static short            HilbertDirection;
  39. static short            vBlockSize;
  40. static short            hBlockSize;
  41. static RgnHandle    boundsRgn;
  42.  
  43. pascal void HilbertRecurseReversed(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr,
  44.     Rect boundsRect, short level, short whichpattern, short* x, short* y)
  45. {
  46.     short            i;
  47.     Rect            source;
  48.     
  49.     for (i=0; i<11; i++)
  50.     {
  51.         switch (HilbertPattern[whichpattern][i])
  52.         {
  53.             case 0x01:     /* turn left */
  54.                 HilbertDirection--;
  55.                 if (HilbertDirection<0) HilbertDirection=3;
  56.                 break;
  57.             case 0x02:     /* turn right */
  58.                 HilbertDirection++;
  59.                 if (HilbertDirection==4) HilbertDirection=0;
  60.                 break;
  61.             case 0x00:    /* draw */
  62.                 StartTiming();
  63.                 SetRect(&source, theWindowWidth-*x-hBlockSize, *y-vBlockSize,
  64.                     theWindowWidth-*x, *y);
  65.                 OffsetRect(&source, boundsRect.left, boundsRect.top);
  66.                 CopyBits(&(sourceGrafPtr->portBits), &(destGrafPtr->portBits),
  67.                                 &source, &source, 0, boundsRgn);
  68.                 switch (HilbertDirection)
  69.                 {
  70.                     case 0:
  71.                         *x+=hBlockSize;
  72.                         break;
  73.                     case 1:
  74.                         *y-=vBlockSize;
  75.                         break;
  76.                     case 2:
  77.                         *x-=hBlockSize;
  78.                         break;
  79.                     case 3:
  80.                         *y+=vBlockSize;
  81.                         break;
  82.                 }
  83.                 TimeCorrection(CorrectTime);
  84.                 break;
  85.             case 0x42:    /* call X */
  86.                 if (level>1)
  87.                     HilbertRecurseReversed(sourceGrafPtr,destGrafPtr,boundsRect,level-1,0,x,y);
  88.                 break;
  89.             case 0x69:    /* call Y */
  90.                 if (level>1)
  91.                     HilbertRecurseReversed(sourceGrafPtr,destGrafPtr,boundsRect,level-1,1,x,y);
  92.                 break;
  93.         }
  94.     }
  95. }
  96.  
  97. pascal short HilbertWipeReversed(GrafPtr sourceGrafPtr, GrafPtr destGrafPtr, Rect boundsRect)
  98. {
  99.     short            curx, cury;
  100.     short            answer, i;
  101.     
  102.     answer=1;
  103.     for (i=0; i<RecursionLevel; i++)
  104.         answer*=2;
  105.     vBlockSize=1+theWindowHeight/answer;    /* used to be 20 */
  106.     hBlockSize=1+theWindowWidth/answer;        /* used to be 32 */
  107.     HilbertDirection=0;
  108.     boundsRgn=NewRgn();
  109.     RectRgn(boundsRgn, &boundsRect);
  110.     cury=theWindowHeight;
  111.     curx=0;
  112.     HilbertRecurseReversed(sourceGrafPtr,destGrafPtr,boundsRect,RecursionLevel,0,&curx,&cury);
  113.     CopyBits(&(sourceGrafPtr->portBits), &(destGrafPtr->portBits),
  114.                 &boundsRect, &boundsRect, 0, 0L);  /* in case we missed any bits */
  115.     
  116.     DisposeRgn(boundsRgn);
  117.     
  118.     return 0;
  119. }
  120.